package com.pet.platform.service.impl;

import com.pet.platform.dto.AddressDTO;
import com.pet.platform.entity.Address;
import com.pet.platform.exception.BusinessException;
import com.pet.platform.mapper.AddressMapper;
import com.pet.platform.service.AddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 地址服务实现类
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<AddressDTO> getAddressList(Long userId) {
        List<Address> addresses = addressMapper.findByUserId(userId);
        return addresses.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public AddressDTO createAddress(Long userId, AddressDTO addressDTO) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO, address);
        address.setUserId(userId);
        address.setCreateTime(LocalDateTime.now());
        address.setUpdateTime(LocalDateTime.now());
        
        // 如果是默认地址，先重置用户的所有默认地址
        if (Boolean.TRUE.equals(addressDTO.getIsDefault())) {
            addressMapper.resetDefault(userId);
        }
        
        addressMapper.insert(address);
        addressDTO.setId(address.getId());
        return addressDTO;
    }

    @Override
    @Transactional
    public AddressDTO updateAddress(Long userId, Long id, AddressDTO addressDTO) {
        Address existingAddress = addressMapper.findById(id);
        
        if (existingAddress == null) {
            throw new BusinessException("地址不存在");
        }
        
        if (!existingAddress.getUserId().equals(userId)) {
            throw new BusinessException("无权限修改此地址");
        }
        
        // 更新地址信息
        BeanUtils.copyProperties(addressDTO, existingAddress);
        existingAddress.setId(id);  // 确保ID不变
        existingAddress.setUserId(userId);  // 确保用户ID不变
        existingAddress.setUpdateTime(LocalDateTime.now());
        
        // 如果是默认地址，先重置用户的所有默认地址
        if (Boolean.TRUE.equals(addressDTO.getIsDefault())) {
            addressMapper.resetDefault(userId);
        }
        
        addressMapper.update(existingAddress);
        
        return convertToDTO(existingAddress);
    }

    @Override
    @Transactional
    public void deleteAddress(Long userId, Long id) {
        Address existingAddress = addressMapper.findById(id);
        
        if (existingAddress == null) {
            throw new BusinessException("地址不存在");
        }
        
        if (!existingAddress.getUserId().equals(userId)) {
            throw new BusinessException( "无权限删除此地址");
        }
        
        addressMapper.deleteById(id);
    }

    @Override
    public AddressDTO getAddressDetail(Long userId, Long id) {
        Address address = addressMapper.findById(id);
        
        if (address == null) {
            throw new BusinessException("地址不存在");
        }
        
        if (!address.getUserId().equals(userId)) {
            throw new BusinessException("无权限查看此地址");
        }
        
        return convertToDTO(address);
    }
    
    /**
     * 将实体对象转换为DTO
     *
     * @param address 地址实体
     * @return 地址DTO
     */
    private AddressDTO convertToDTO(Address address) {
        if (address == null) {
            return null;
        }
        
        AddressDTO addressDTO = new AddressDTO();
        BeanUtils.copyProperties(address, addressDTO);
        return addressDTO;
    }
} 